/*
 * Copyright 2006 Berry Crawford
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.judo.database.orm.mappers;

import java.sql.Connection;
import java.sql.DriverManager;

import org.jdom.Element;
import org.judo.database.ConnectionFactory;
import org.judo.database.JudoDBConnection;
import org.judo.database.exceptions.DatabaseConnectionException;
import org.judo.database.exceptions.InvalidDBConfigException;
import org.judo.util.Util;

public class SimpleJDBCConnectionFactory implements ConnectionFactory
{
	protected String driver;
	protected String url;
	protected String user;
	protected String pass;
	
	public JudoDBConnection getConnection() throws DatabaseConnectionException
	{
		SimpleJDBCConnection con = null;
		
		try
		{
			Class.forName(driver);
			Connection connection = DriverManager.getConnection(url,user,pass);
			connection.setAutoCommit(false);
			con = new SimpleJDBCConnection(connection);
		}
		catch(Exception e)
		{
			e.printStackTrace();
			throw new DatabaseConnectionException("Could not connect to simple JDBC connection: "+e);
		}
		
		return con;
	}

	public void init(Element config) throws InvalidDBConfigException
	{
		String driver = config.getChildText("driver");
		String url = config.getChildText("url");
		String user = config.getChildText("user");
		String pass = config.getChildText("pass");
		
		setDriver(driver);
		setPass(pass);
		setUser(user);
		setUrl(url);
	}
	
	public String getDriver()
	{
		return driver;
	}

	public void setDriver(String driver)
	{
		this.driver = driver;
	}

	public String getPass()
	{
		return pass;
	}

	public void setPass(String pass)
	{
		this.pass = pass;
	}

	public String getUrl()
	{
		return url;
	}

	public void setUrl(String url)
	{
		this.url = url;
	}

	public String getUser()
	{
		return user;
	}

	public void setUser(String user)
	{
		this.user = user;
	}
}
